var wipsDialog = function (element, options)
{	
	jQuery(function($) 
	{
		this.options = {};
		this.hasData = false;
		this.onSave = function(result)
		{
		};
		
		this.removeClass = function (className)
		{
			if (this.hasData)
			{
				for (var d in this.options['data']) 
				{
					$('#' + d).removeClass(className);
				}
			}
		};
		
		this.buidValues = function()
		{
			if (this.hasData)
			{
				var results = new Object(); // or just {}				
				for (var d in this.options['data']) 
				{
					results[d] = $('#' + d).val();
				}
				return results;
			}
			return false;
		};
		
		this.init = function(element,options)
		{
			this.element = element;		
			for (var o in options) {		
				if (typeof options[o] == 'function')
				{				
					var tex_function = 'this.'+ o + '= ' + options[o];
					eval(tex_function);		
				}else
		        if (options.hasOwnProperty(o)) 
		        {
		            this.options[o] = options[o];
		        }
		    };
			this.dialog = $("<div/>", 
			{
				'title':'Dialog',
				'html':'<p class="validateTips">All form fields are required.</p>'
			});	
			
			if (typeof this.options['data'] == 'object')
			{
				this.hasData = true;
				var fieldset = $('<fieldset/>');
				this.dialog.append($('<form/>').append(fieldset));
				for (var d in this.options['data'])
				{					
					fieldset.append(
						$('<label/>',
						{
							'for': d,
							'html': Utilities.capitaliseFirstLetter(d)
						})
					);
					fieldset.append(
						$('<input/>',
						{
							'class':'text ui-widget-content ui-corner-all',
							'type' : 'text',
							'name' : d,
							'id': d,
							'value' : this.options['data'][d]
						})
					);					
				}			
			}			
			
			var selft  = this;
			if (typeof element == 'object')
			{
				this.element.bind('click', function() 
				{
					selft.dialog.dialog(
					{
						modal: true,
						height: 300,
						width: 350,
						buttons: 
						{
							save: function() 
							{
								var bValid = true;
								selft.removeClass('ui-state-error');
								
								if (selft.hasData)
								{
									for (var d in selft.options['data']) {
										var id = '#' + d;										
										var result = Utilities.checkLength($(id).val(), 1, 0);										
										if (!result)
										{
											$(id).addClass('ui-state-error');
										}
										bValid = bValid && result;
									}
								}
								
								if ( bValid )
								{
									selft.onSave(selft.buidValues());
									$(this).dialog( "close" );
								}
							},
							close:function() 
							{
								$(this).dialog( "close" );
							}
						}
					});
				});
			}
		};
		this.init(element,options);
	});
}

//Demo
$(document).ready(function() 
{
	var a = new wipsDialog($('#abac'),
	{
		data :
		{
			'weight': '',
			'height': ''
		},
		onSave : function(result)
		{
			console.log(result);
		}
	});
});